//
//  ACMemoryStorage.m
//  ACCache_Example
//
//  Created by autohome on 2021/8/7.
//  Copyright © 2021 CaiXiang. All rights reserved.
//

#import "ACMemoryCache.h"
#import <UIKit/UIKit.h>
#import <pthread.h>
#import "ACDoubleLinkedList.h"
#import "ACLinkedNode.h"
#import "ACLFUCache.h"

@implementation ACMemoryCache{
    ACLFUCache *_lfuCache;
}

- (nullable instancetype)initWithName:(NSString *)name {
    self = [super init];
    if (self) {
        _name = name;
        ACLFUCache *lfuCache = [[ACLFUCache alloc] init];
        _lfuCache = lfuCache;
        __weak typeof(self) _self = self;
        _lfuCache.didReceiveMemoryWarningBlock = ^(ACLFUCache *cache) {
            __strong typeof(_self) self = _self;
            !self.didReceiveMemoryWarningBlock?:self.didReceiveMemoryWarningBlock(self);
        };
        _lfuCache.didEnterBackgroundBlock = ^(ACLFUCache *cache) {
            __strong typeof(_self) self = _self;
            !self.didEnterBackgroundBlock?:self.didEnterBackgroundBlock(self);
        };
    }
    return self;
}

- (nullable id)objectForKey:(nonnull id)key {
    return  [_lfuCache objectForKey:key];
}

- (void)setObject:(nullable id)object forKey:(nonnull id)key {
    [_lfuCache setObject:object forKey:key];
}

- (void)setObject:(nullable id)object forKey:(nonnull id)key cost:(NSUInteger)cost {
    [_lfuCache setObject:object forKey:key cost:cost];
}

- (void)removeObjectForKey:(nonnull id)key {
    [_lfuCache removeObjectForKey:key];
}

- (void)removeAllObjects {
    [_lfuCache removeAllObjects];
}

- (void)setCountLimit:(NSUInteger)countLimit {
    _countLimit = countLimit;
    if (_lfuCache && _lfuCache.countLimit != countLimit) {
        _lfuCache.countLimit = countLimit;
        [_lfuCache trimToCount:countLimit];
    }
}

- (void)setCostLimit:(NSUInteger)costLimit {
    _costLimit = costLimit;
    if (_lfuCache && _lfuCache.costLimit != costLimit) {
        _lfuCache.costLimit = costLimit;
        [_lfuCache trimToCost:costLimit];
    }
}

- (void)setShouldRemoveAllObjectsWhenEnteringBackground:(BOOL)shouldRemoveAllObjectsWhenEnteringBackground {
    _shouldRemoveAllObjectsWhenEnteringBackground = shouldRemoveAllObjectsWhenEnteringBackground;
    _lfuCache.shouldRemoveAllObjectsWhenEnteringBackground = shouldRemoveAllObjectsWhenEnteringBackground;
}

- (void)setShouldRemoveAllObjectsOnMemoryWarning:(BOOL)shouldRemoveAllObjectsOnMemoryWarning {
    _shouldRemoveAllObjectsOnMemoryWarning = shouldRemoveAllObjectsOnMemoryWarning;
    _lfuCache.shouldRemoveAllObjectsOnMemoryWarning = shouldRemoveAllObjectsOnMemoryWarning;
}


- (BOOL)containsObjectForKey:(id)key {
    return [_lfuCache containsObjectForKey:key];
}

- (NSArray *)allkeys {
    return [_lfuCache allkeys];
}

- (NSArray *)allValues {
    return [_lfuCache allValues];
}

- (NSUInteger)totalCount {
    return _lfuCache.totalCount;
}

- (NSUInteger)totalCost {
    return _lfuCache.totalCost;
}

- (void)dealloc {
#if DEBUG
    NSLog(@"%s",__func__);
#endif
}

@end
